package study.nosql.redis.controller;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.eggs.result.Result;
import study.eggs.result.Results;

@Slf4j
@RestController
@RequestMapping("/pub")
public class _2_PubSubController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/send")
    public Result pub(@RequestBody PubRequest request) {
        String channel = request.getChannel();
        String message = request.getMessage();
        redisTemplate.convertAndSend(channel, message);
        return Results.ok();
    }

    @Data
    public static class PubRequest {
        private String channel;
        private String message;
    }
}
